﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Data;
using System.IO;
using System.Text;
using ITPCTaxFormModel;
using System.Xml;
using Soundex;
public partial class jFetchingResult : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int proid=0,procateid=0;
            if (!Request["ProductCategoryID"].Equals(""))
                procateid = Convert.ToInt32(Request["ProductCategoryID"]);
            if (!Request["ProductID"].Equals(""))
                proid = Convert.ToInt32(Request["ProductID"]);
            string textsearch = "";
            if (!Request["TextSearch"].Equals(""))
                textsearch = Request["TextSearch"];

            FetchResult(proid, procateid, textsearch, 0, 20);

            //Did you mean
            //FetchDYMResult(textsearch);
        }
    }
    public static string DS2JSON(DataSet ds)
    {
        StringWriter sw = new StringWriter();
        ds.WriteXml(sw, XmlWriteMode.IgnoreSchema);
        XmlDocument xd = new XmlDocument();
        xd.LoadXml(sw.ToString());

        string jsonText = JsonConvert.SerializeXmlNode(xd);
        return jsonText;
    }
    public DataTable createDTProductCategories()
    {
        DataTable dt = new DataTable("ProductCategories");
        DataColumn dc = new DataColumn();
        dc = new DataColumn("ProductCategoryID", System.Type.GetType("System.Int32"));
        dt.Columns.Add(dc);
        dc = new DataColumn("ProductCategoryCode", System.Type.GetType("System.String"));
        dt.Columns.Add(dc);
        dc = new DataColumn("Description", System.Type.GetType("System.String"));
        dt.Columns.Add(dc);
        return dt;
    }
    public DataTable createDTProducts()
    {
        DataTable dt1 = new DataTable("Products");
        DataColumn dc = new DataColumn();
        dc = new DataColumn("ProductID", System.Type.GetType("System.Int32"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("ProductCode", System.Type.GetType("System.String"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("Description", System.Type.GetType("System.String"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("Unit", System.Type.GetType("System.String"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("Special", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("TT", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("CEPT", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("ACFTA_TS", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("ACFTA_NoneApply", System.Type.GetType("System.String"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("AKFTA_TS", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("AKFTA_NoneApply", System.Type.GetType("System.String"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("VJEPA", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("AANZFTA", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("AIFTA_2010", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("AIFTA_2011", System.Type.GetType("System.Double"));
        dt1.Columns.Add(dc);
        dc = new DataColumn("VAT", System.Type.GetType("System.String"));
        dt1.Columns.Add(dc);

        return dt1;
    }
    public DataTable createDTDYM()
    {
        DataTable dt = new DataTable("DYM");
        DataColumn dc = new DataColumn();
        dc = new DataColumn("DYM_Words", System.Type.GetType("System.String"));
        dt.Columns.Add(dc);
        return dt;
    }
    public static string DT2JSON(DataTable dt)
    {
        StringBuilder jsonStringBuilder = new StringBuilder();
        StringWriter jsonStringWriter = new StringWriter(jsonStringBuilder);

        JsonWriter jsonWriter = new JsonTextWriter(jsonStringWriter);

        if (dt != null && dt.Rows.Count > 0)
        {
            jsonWriter.Formatting = Newtonsoft.Json.Formatting.None;

            jsonWriter.WriteStartArray();
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                jsonWriter.WriteStartObject();
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {
                    jsonWriter.WritePropertyName(dt.Columns[j].ColumnName.ToString().ToLower());
                    jsonWriter.WriteValue(dt.Rows[i][j].ToString());
                }
                jsonWriter.WriteEndObject();
            }
            jsonWriter.WriteEndArray();

            return jsonStringBuilder.ToString();
        }
        else
        {
            return null;
        }
    }
    public string searchProduct(ITPCTaxFormEntities entity_context, int proid)
    {
        using (entity_context)
        {
            var products = entity_context.Product.Where(p => p.ProductID == proid).OrderBy(p => p.ProductCode).Select(p => p).ToList();

            DataSet ds = new DataSet();
            DataTable dt = createDTProducts();
            DataTable dt1 = createDTProductCategories();
            DataColumn dc = new DataColumn();

            // ProductCategory must be null
            DataRow dr1 = dt1.NewRow();
            dr1["ProductCategoryID"] = DBNull.Value;
            dr1["ProductCategoryCode"] = DBNull.Value;
            dr1["Description"] = DBNull.Value;
            dt1.Rows.Add(dr1);
            ds.Tables.Add(dt1); //add Table ProductCattegories to DataSet
            dt1 = null;

            if (products.Count > 1 && products != null)
            {
                foreach (var p in products)
                {
                    DataRow dr = dt.NewRow();
                    dr["ProductID"] = p.ProductID;
                    dr["ProductCode"] = p.ProductCode;
                    dr["Description"] = p.Description;
                    dr["Unit"] = p.Unit == null ? DBNull.Value.ToString() : p.Unit;
                    dr["Special"] = p.Special.HasValue ? p.Special : 0;
                    dr["TT"] = p.TT.HasValue ? p.TT : 0;
                    dr["CEPT"] = p.CEPT.HasValue ? p.CEPT : 0;
                    dr["ACFTA_TS"] = p.ACFTA_TS.HasValue ? p.ACFTA_TS : 0;
                    dr["ACFTA_NoneApply"] = p.ACFTA_NoneApply == null ? DBNull.Value.ToString() : p.ACFTA_NoneApply;
                    dr["AKFTA_TS"] = p.AKFTA_TS.HasValue ? p.AKFTA_TS : 0;
                    dr["AKFTA_NoneApply"] = p.AKFTA_NoneApply == null ? DBNull.Value.ToString() : p.AKFTA_NoneApply;
                    dr["VJEPA"] = p.VJEPA.HasValue ? p.VJEPA : 0;
                    dr["AANZFTA"] = p.AANZFTA.HasValue ? p.AANZFTA : 0;
                    dr["AIFTA_2010"] = p.AIFTA_2010.HasValue ? p.AIFTA_2010 : 0;
                    dr["AIFTA_2011"] = p.AIFTA_2011.HasValue ? p.AIFTA_2011 : 0;
                    dr["VAT"] = p.VAT == null ? DBNull.Value.ToString() : p.VAT;
                    dt.Rows.Add(dr);
                }
                ds.Tables.Add(dt); //add Table ProductCattegories to DataSet
                dt = null;
                return DS2JSON(ds);
            }
            else if (products.Count.Equals(1))
            {
                DataRow dr = dt.NewRow();
                dr["ProductID"] = products[0].ProductID;
                dr["ProductCode"] = products[0].ProductCode;
                dr["Description"] = products[0].Description;
                dr["Unit"] = products[0].Unit == null ? "" : products[0].Unit;
                dr["Special"] = products[0].Special.HasValue ? products[0].Special : 0;
                dr["TT"] = products[0].TT.HasValue ? products[0].TT : 0;
                dr["CEPT"] = products[0].CEPT.HasValue ? products[0].CEPT : 0;
                dr["ACFTA_TS"] = products[0].ACFTA_TS.HasValue ? products[0].ACFTA_TS : 0;
                dr["ACFTA_NoneApply"] = products[0].ACFTA_NoneApply == null ? "" : products[0].ACFTA_NoneApply;
                dr["AKFTA_TS"] = products[0].AKFTA_TS.HasValue ? products[0].AKFTA_TS : 0;
                dr["AKFTA_NoneApply"] = products[0].AKFTA_NoneApply == null ? "" : products[0].AKFTA_NoneApply;
                dr["VJEPA"] = products[0].VJEPA.HasValue ? products[0].VJEPA : 0;
                dr["AANZFTA"] = products[0].AANZFTA.HasValue ? products[0].AANZFTA : 0;
                dr["AIFTA_2010"] = products[0].AIFTA_2010.HasValue ? products[0].AIFTA_2010 : 0;
                dr["AIFTA_2011"] = products[0].AIFTA_2011.HasValue ? products[0].AIFTA_2011 : 0;
                dr["VAT"] = products[0].VAT == null ? "" : products[0].VAT;

                dt.Rows.Add(dr);
                ds.Tables.Add(dt);

                return DS2JSON(ds);
            }
            else
            {
                DataRow dr = dt.NewRow();
                dr["ProductID"] = DBNull.Value;
                dr["ProductCode"] = DBNull.Value;
                dr["Description"] = DBNull.Value;
                dr["Unit"] = DBNull.Value;
                dr["Special"] = DBNull.Value;
                dr["TT"] = DBNull.Value;
                dr["CEPT"] = DBNull.Value;
                dr["ACFTA_TS"] = DBNull.Value;
                dr["ACFTA_NoneApply"] = DBNull.Value;
                dr["AKFTA_TS"] = DBNull.Value;
                dr["AKFTA_NoneApply"] = DBNull.Value;
                dr["VJEPA"] = DBNull.Value;
                dr["AANZFTA"] = DBNull.Value;
                dr["AIFTA_2010"] = DBNull.Value;
                dr["AIFTA_2011"] = DBNull.Value;
                dr["VAT"] = DBNull.Value;
                dt.Rows.Add(dr);
                ds.Tables.Add(dt); //add Table Products to DataSet
                dt = null;

                return DS2JSON(ds);
            }
        }
    }
    public string searchText(ITPCTaxFormEntities entity_context, string textsearch)
    {
        using (entity_context)
        {
            var productCategories = entity_context.ProductCategory.Where(pc => pc.Description.Contains(textsearch) || pc.ProductCategoryCode.Contains(textsearch)).OrderBy(pc => pc.ProductCategoryCode).Select(pc => pc).ToList();

            DataSet ds = new DataSet();
            DataTable dt1 = createDTProductCategories();
            DataColumn dc = new DataColumn();
            bool match = false;
            if (productCategories.Count > 1 && productCategories != null)
            {
                foreach (var productcategory in productCategories)
                {
                    DataRow dr = dt1.NewRow();
                    dr["ProductCategoryID"] = productcategory.ProductCategoryID;
                    dr["ProductCategoryCode"] = productcategory.ProductCategoryCode;
                    dr["Description"] = productcategory.Description;
                    dt1.Rows.Add(dr);
                }
                ds.Tables.Add(dt1); //add Table ProductCattegories to DataSet
                match = true;
            }
            else if (productCategories.Count.Equals(1))
            {
                DataRow dr = dt1.NewRow();
                dr["ProductCategoryID"] = productCategories[0].ProductCategoryID;
                dr["ProductCategoryCode"] = productCategories[0].ProductCategoryCode;
                dr["Description"] = productCategories[0].Description;
                dt1.Rows.Add(dr);
                ds.Tables.Add(dt1); //add Table ProductCattegories to DataSet
                dt1 = null;
                match = true;
            }
            else
            {
                //DataRow dr = dt1.NewRow();
                //dr["ProductCategoryID"] = DBNull.Value;
                //dr["ProductCategoryCode"] = DBNull.Value;
                //dr["Description"] = DBNull.Value;
                //dt1.Rows.Add(dr);
                //ds.Tables.Add(dt1); //add Table ProductCattegories to DataSet
            }

            var products = entity_context.Product.Where(p => p.Description.Contains(textsearch) || p.ProductCode.Contains(textsearch)).OrderBy(p => p.ProductCode).Select(p => p).ToList();
            DataTable dt = createDTProducts();
            if (products.Count > 1 && products != null)
            {
                foreach (var p in products)
                {
                    DataRow dr = dt.NewRow();
                    dr["ProductID"] = p.ProductID;
                    dr["ProductCode"] = p.ProductCode;
                    dr["Description"] = p.Description;
                    dr["Unit"] = p.Unit == null ? "" : p.Unit;
                    dr["Special"] = p.Special.HasValue ? p.Special : 0;
                    dr["TT"] = p.TT.HasValue?p.TT:0;
                    dr["CEPT"] = p.CEPT.HasValue?p.CEPT:0;
                    dr["ACFTA_TS"] = p.ACFTA_TS.HasValue?p.ACFTA_TS:0;
                    dr["ACFTA_NoneApply"] = p.ACFTA_NoneApply == null ? DBNull.Value.ToString() : p.ACFTA_NoneApply;
                    dr["AKFTA_TS"] = p.AKFTA_TS.HasValue?p.AKFTA_TS:0;
                    dr["AKFTA_NoneApply"] = p.AKFTA_NoneApply == null ? DBNull.Value.ToString() : p.AKFTA_NoneApply;
                    dr["VJEPA"] = p.VJEPA.HasValue?p.VJEPA:0;
                    dr["AANZFTA"] = p.AANZFTA.HasValue?p.AANZFTA:0;
                    dr["AIFTA_2010"] = p.AIFTA_2010.HasValue?p.AIFTA_2010:0;
                    dr["AIFTA_2011"] = p.AIFTA_2011.HasValue?p.AIFTA_2011:0;
                    dr["VAT"] = p.VAT == null ? "" : p.VAT;
                    dt.Rows.Add(dr);
                }
                ds.Tables.Add(dt); //add Table ProductCattegories to DataSet
                dt = null;
                match = true;
            }
            else if (products.Count.Equals(1))
            {
                DataRow dr = dt.NewRow();
                dr["ProductID"] = products[0].ProductID;
                dr["ProductCode"] = products[0].ProductCode;
                dr["Description"] = products[0].Description;
                dr["Unit"] = products[0].Unit == null ? "" : products[0].Unit;
                dr["Special"] = products[0].Special.HasValue ? products[0].Special : 0;
                dr["TT"] = products[0].TT.HasValue ? products[0].TT : 0;
                dr["CEPT"] = products[0].CEPT.HasValue ? products[0].CEPT : 0;
                dr["ACFTA_TS"] = products[0].ACFTA_TS.HasValue ? products[0].ACFTA_TS : 0;
                dr["ACFTA_NoneApply"] = products[0].ACFTA_NoneApply == null ? "" : products[0].ACFTA_NoneApply;
                dr["AKFTA_TS"] = products[0].AKFTA_TS.HasValue ? products[0].AKFTA_TS : 0;
                dr["AKFTA_NoneApply"] = products[0].AKFTA_NoneApply == null ? "" : products[0].AKFTA_NoneApply;
                dr["VJEPA"] = products[0].VJEPA.HasValue ? products[0].VJEPA : 0;
                dr["AANZFTA"] = products[0].AANZFTA.HasValue ? products[0].AANZFTA : 0;
                dr["AIFTA_2010"] = products[0].AIFTA_2010.HasValue ? products[0].AIFTA_2010 : 0;
                dr["AIFTA_2011"] = products[0].AIFTA_2011.HasValue ? products[0].AIFTA_2011 : 0;
                dr["VAT"] = products[0].VAT == null ? "" : products[0].VAT;

                dt.Rows.Add(dr);
                ds.Tables.Add(dt);
                dt = null;
                match = true;
            }
            else
            {
                //DataRow dr = dt.NewRow();
                //dr["ProductID"] = DBNull.Value;
                //dr["ProductCode"] = DBNull.Value;
                //dr["Description"] = DBNull.Value;
                //dr["Unit"] = DBNull.Value;
                //dr["Special"] = DBNull.Value;
                //dr["TT"] = DBNull.Value;
                //dr["CEPT"] = DBNull.Value;
                //dr["ACFTA_TS"] = DBNull.Value;
                //dr["ACFTA_NoneApply"] = DBNull.Value;
                //dr["AKFTA_TS"] = DBNull.Value;
                //dr["AKFTA_NoneApply"] = DBNull.Value;
                //dr["VJEPA"] = DBNull.Value;
                //dr["AANZFTA"] = DBNull.Value;
                //dr["AIFTA_2010"] = DBNull.Value;
                //dr["AIFTA_2011"] = DBNull.Value;
                //dr["VAT"] = DBNull.Value;
                //dt.Rows.Add(dr);
                //ds.Tables.Add(dt); //add Table Products to DataSet
                //dt = null;
            }

            /*
            DYM on Demand
            */
            if (match.Equals(false))
            {
                DataTable dt2 = createDTDYM();
                DataRow dr2 = dt2.NewRow();
                dr2["DYM_Words"] = FetchDYMResult(textsearch, entity_context);
                dt2.Rows.Add(dr2);
                ds.Tables.Add(dt2);
            }
            return DS2JSON(ds);
        }
    }

    protected void FetchResult(int proid, int procateid, string textsearch, int skip, int take)
    {
        using (var entity_context = new ITPCTaxFormEntities())
        {
            if (!procateid.Equals(0))
            {
                //select all childs of the ProductCategory
                var productcategories = entity_context.ProductCategory.Where(pc => pc.ParentProductCategoryID == procateid).OrderBy(pc => pc.ProductCategoryCode).Select(pc => new { pc.ProductCategoryID, pc.ProductCategoryCode, pc.Description }).Skip(skip).Take(take).ToList();

                //select all products in ProductCategory
                var products = (from p in entity_context.Product
                               join pc in entity_context.ProductCategory on p.ProductCategoryID equals pc.ProductCategoryID
                               where pc.ProductCategoryID == procateid
                               orderby p.ProductCode
                               select p).ToList();

                DataSet ds = new DataSet();
                DataTable dt = createDTProductCategories();
                if (productcategories.Count > 1 && productcategories != null)
                {
                    foreach (var productcategory in productcategories)
                    {
                        DataRow dr = dt.NewRow();
                        dr["ProductCategoryID"] = productcategory.ProductCategoryID;
                        dr["ProductCategoryCode"] = productcategory.ProductCategoryCode;
                        dr["Description"] = productcategory.Description;
                        dt.Rows.Add(dr);
                    }
                    ds.Tables.Add(dt); //add Table ProductCattegories to DataSet
                    dt = null;
                }
                else if (productcategories.Count.Equals(1))
                {
                    DataRow dr = dt.NewRow();
                    dr["ProductCategoryID"] = productcategories[0].ProductCategoryID;
                    dr["ProductCategoryCode"] = productcategories[0].ProductCategoryCode;
                    dr["Description"] = productcategories[0].Description;
                    dt.Rows.Add(dr);
                    ds.Tables.Add(dt); //add Table ProductCattegories to DataSet
                    dt = null;
                }
                else
                {
                    DataRow dr = dt.NewRow();
                    dr["ProductCategoryID"] = DBNull.Value;
                    dr["ProductCategoryCode"] = DBNull.Value;
                    dr["Description"] = DBNull.Value;
                    dt.Rows.Add(dr);
                    ds.Tables.Add(dt); //add Table ProductCattegories to DataSet
                    dt = null;
                }

                DataTable dt1 = createDTProducts();
                if (products.Count > 1 && products != null)
                {

                    foreach (var p in products)
                    {
                        DataRow dr = dt1.NewRow();
                        dr["ProductID"] = p.ProductID;
                        dr["ProductCode"] = p.ProductCode;
                        dr["Description"] = p.Description;
                        dr["Unit"] = p.Unit == null ? DBNull.Value.ToString() : p.Unit;
                        dr["Special"] = p.Special.HasValue ? p.Special : 0;
                        dr["TT"] = p.TT.HasValue ? p.TT : 0;
                        dr["CEPT"] = p.CEPT.HasValue ? p.CEPT : 0;
                        dr["ACFTA_TS"] = p.ACFTA_TS.HasValue ? p.ACFTA_TS : 0;
                        dr["ACFTA_NoneApply"] = p.ACFTA_NoneApply == null ? DBNull.Value.ToString() : p.ACFTA_NoneApply;
                        dr["AKFTA_TS"] = p.AKFTA_TS.HasValue ? p.AKFTA_TS : 0;
                        dr["AKFTA_NoneApply"] = p.AKFTA_NoneApply == null ? DBNull.Value.ToString() : p.AKFTA_NoneApply;
                        dr["VJEPA"] = p.VJEPA.HasValue ? p.VJEPA : 0;
                        dr["AANZFTA"] = p.AANZFTA.HasValue ? p.AANZFTA : 0;
                        dr["AIFTA_2010"] = p.AIFTA_2010.HasValue ? p.AIFTA_2010 : 0;
                        dr["AIFTA_2011"] = p.AIFTA_2011.HasValue ? p.AIFTA_2011 : 0;
                        dr["VAT"] = p.VAT == null ? DBNull.Value.ToString() : p.VAT;

                        dt1.Rows.Add(dr);
                    }
                    ds.Tables.Add(dt1); //add Table Products to DataSet
                    dt1 = null;
                }
                else if (products.Count.Equals(1))
                {
                    DataRow dr = dt1.NewRow();
                    dr["ProductID"] = products[0].ProductID;
                    dr["ProductCode"] = products[0].ProductCode;
                    dr["Description"] = products[0].Description;
                    dr["Unit"] = products[0].Unit == null ? "" : products[0].Unit;
                    dr["Special"] = products[0].Special.HasValue ? products[0].Special : 0;
                    dr["TT"] = products[0].TT.HasValue ? products[0].TT : 0;
                    dr["CEPT"] = products[0].CEPT.HasValue ? products[0].CEPT : 0;
                    dr["ACFTA_TS"] = products[0].ACFTA_TS.HasValue ? products[0].ACFTA_TS : 0;
                    dr["ACFTA_NoneApply"] = products[0].ACFTA_NoneApply == null ? "" : products[0].ACFTA_NoneApply;
                    dr["AKFTA_TS"] = products[0].AKFTA_TS.HasValue ? products[0].AKFTA_TS : 0;
                    dr["AKFTA_NoneApply"] = products[0].AKFTA_NoneApply == null ? "" : products[0].AKFTA_NoneApply;
                    dr["VJEPA"] = products[0].VJEPA.HasValue ? products[0].VJEPA : 0;
                    dr["AANZFTA"] = products[0].AANZFTA.HasValue ? products[0].AANZFTA : 0;
                    dr["AIFTA_2010"] = products[0].AIFTA_2010.HasValue ? products[0].AIFTA_2010 : 0;
                    dr["AIFTA_2011"] = products[0].AIFTA_2011.HasValue ? products[0].AIFTA_2011 : 0;
                    dr["VAT"] = products[0].VAT == null ? "" : products[0].VAT;

                    dt1.Rows.Add(dr);
                    ds.Tables.Add(dt1);
                    dt1 = null;
                }
                else
                {
                    DataRow dr = dt1.NewRow();
                    dr["ProductID"] = DBNull.Value;
                    dr["ProductCode"] = DBNull.Value;
                    dr["Description"] = DBNull.Value;
                    dr["Unit"] = DBNull.Value;
                    dr["Special"] = DBNull.Value;
                    dr["TT"] = DBNull.Value;
                    dr["CEPT"] = DBNull.Value;
                    dr["ACFTA_TS"] = DBNull.Value;
                    dr["ACFTA_NoneApply"] = DBNull.Value;
                    dr["AKFTA_TS"] = DBNull.Value;
                    dr["AKFTA_NoneApply"] = DBNull.Value;
                    dr["VJEPA"] = DBNull.Value;
                    dr["AANZFTA"] = DBNull.Value;
                    dr["AIFTA_2010"] = DBNull.Value;
                    dr["AIFTA_2011"] = DBNull.Value;
                    dr["VAT"] = DBNull.Value;
                    dt1.Rows.Add(dr);
                    ds.Tables.Add(dt1); //add Table Products to DataSet
                    dt1 = null;
                }

                /*
                 FINALLY convert DataSet to JSON and write response
                 */
                string strJS = DS2JSON(ds);
                Response.Write(strJS);
                
            }
            else if (!proid.Equals(0)) //Matching Product ==> Search products by proid
            {
                string strJS = searchProduct(entity_context,proid);
                Response.Write(strJS);
            }
            else // Search the text in the user input ==> ==> Search products and productCategories by textsearch
            {
                string strJS = searchText(entity_context,textsearch);
                Response.Write(strJS);
            }
        }


    }
    protected string FetchDYMResult(string textsearch, ITPCTaxFormEntities entity_context)
    {
        Soundex.Soundex sd = new Soundex.Soundex();
        var strMatch = sd.GetSoundex(textsearch);
        var product = entity_context.Product.Select(p => p.Description).ToList();
        if (product.Count() > 0)
        {
            foreach (var p in product)
            {
                char[] separator = new char[] {' '};
                string[] strSplitArr = p.ToString().Split(separator);
                foreach (var k in strSplitArr)
                {
                    if (sd.GetSoundex(k).Equals(strMatch))
                        return k;
                }
            }
        }
        return null;
    }
}

